/**
 * Created by Administrator on 2017/4/17.
 */
/**
 * Created by Administrator on 2017/3/30.
 */
import sel from '../../../../index.js';

export default {
  init:function () {
    sel.app.controller('api4C',['$scope','$http',function ($scope,$http) {
      $scope.title = 'dem04';
      $http.get('api/tree').then(function (data) {
        $scope.nodes = data.data.data.data.ciNodes;
        $scope.lines = data.data.data.data.ciRltLines;
        $scope.class = data.data.data.data.ciClassInfos;
      })
      ;
      $scope.show = function () {
        //进行数据处理，进行child和node切换
        //转换数据
         for(let i = 0 ; i < $scope.lines.length; i ++) {
          for(let j = 0 ; j < $scope.nodes.length; j++){
            if($scope.lines[i].source == $scope.nodes[j].id) {
              $scope.lines[i].sourceObj = $scope.nodes[j];

            };
            if($scope.lines[i].target == $scope.nodes[j].id) {
              $scope.lines[i].targetObj = $scope.nodes[j];
              $scope.nodes[j].isChild = 1;
            }
          };
          if(!$scope.lines[i].sourceObj.children){
            $scope.lines[i].sourceObj.children = [];
          }
          $scope.lines[i].sourceObj.children.push($scope.lines[i].targetObj);
        };
        //添加展示层级
        function indexCount(obj) {
          if(!obj.children || !obj.children.length){
            return
          }else {
            for(var i = 0 ; i < obj.children.length; i++ ){
              if(obj.children[i].zIndex){
                if(obj.children[i].zIndex > obj.zIndex){
                  indexCount(obj.children[i]);
                }else {
                  obj.children[i].children = null;
                }
              }else {
                obj.children[i].zIndex = obj.zIndex + 1;
                indexCount(obj.children[i]);
              }
            }
          }
        }

        //从数据中剔除某个元素


        var treeArr = [];
        for(var j = 0 ; j < $scope.nodes.length; j++) {
          if(!$scope.nodes[j].isChild) {
            $scope.nodes[j].zIndex = 1;
            indexCount($scope.nodes[j]);
            treeArr.push($scope.nodes[j]);
          }
        };


      //树形结构的布局
        var root = d3.hierarchy(treeArr[0]).sum(function (d) {
          return d.children ? 0: 1
        });
        var treeMap = d3.treemap()
          .tile(d3.treemapBinary )//可以好多中布局treemapSliceDice/treemapBinary /d3.treemapSlice /treemapSquarify
          .size([800,300])
          .round(true)
          .padding(10)
        ;
        treeMap(root);
        console.log(root.descendants());

        d3.select('svg').remove();
        var svg = d3.select('.container').append('svg').style('top','200px');
        var pad = svg.append('g').attr('class','pad');
        var cell = pad.selectAll('g.leaves').data(root.descendants()).enter()
            .append('g')
            .attr('class','leaves')
        .attr('transform',function (d) {
          return "translate(" + d.x0 + "," + d.y0 + ")";
        });
        cell.append('rect')
          .attr('class',function (d) {
              return 'rectCell'+ d.depth
          })
          .attr("width", function(d) {
            if(d.depth){
              return 0
            }
            return d.x1 - d.x0;
          })
          .attr("height", function(d) {
            if(d.depth){
              return 0
            }
            return d.y1 - d.y0;
          })
          .style('fill',function (d) {
            return d.data.classColor || 'blue'
          })
          .attr('stroke',0)
          .on('click',function (d) {
            filters(d)
          })
        ;
        cell.append('title')
          .text(function (d) {
            return d.data.ciCode
          })
        ;



        function filters(data) {
          console.log(data);
          var className = '.rectCell' + (data.depth+1);
          console.log(className);
          d3.selectAll(className).filter(function (d) {
            return d.parent.data.id == data.data.id
          })
          .transition()
          .attr('width',function (d) {
            return d.x1 - d.x0;
          })
          .attr('height',function (d) {
            return d.y1 - d.y0;
          });
        };

      };






      var draw = function () {
        var type = null;
        function _draw() {

        }
        return _draw;
      };





    }]);

  }
}